﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using ForecastParser.Mobile.Model;

namespace ForecastParser.Mobile.Service
{
    class WeatherService
    {
        internal Forecast GetDigitalDwmlForecast(XElement root)
        {
            var forecast = new Forecast();
            forecast.Location.Altitude = Int32.Parse(root.Element("data").Element("location").Element("height").Value);
            forecast.Location.City = root.Element("data").Element("location").Element("city").Value;
            forecast.Location.State = root.Element("data").Element("location").Element("city").Attribute("state").Value;

            var startTimeNodes = root.Element("data").Element("time-layout").Elements("start-valid-time").ToList();
            var endTimeNodes = root.Element("data").Element("time-layout").Elements("end-valid-time").ToList();
            var cloudPathNodes = root.Element("data").Element("parameters").Element("cloud-amount").Elements("value").ToList();
            var duePointTempNodes = root.Element("data").Element("parameters").Elements("temperature").Where(e => e.Attribute("type").Value.Equals("dew point")).First().Elements("value").ToList();
            var gustingWindSpeedNodes = root.Element("data").Element("parameters").Elements("wind-speed").Where(e => e.Attribute("type").Value.Equals("gust")).First().Elements("value").ToList();
            var humidityNodes = root.Element("data").Element("parameters").Element("humidity").Elements("value").ToList();
            var percipitationNodes = root.Element("data").Element("parameters").Element("probability-of-precipitation").Elements("value").ToList();
            var sustainedWindNodes = root.Element("data").Element("parameters").Elements("wind-speed").Where(e => e.Attribute("type").Value.Equals("sustained")).First().Elements("value").ToList();
            var temperatureNodes = root.Element("data").Element("parameters").Elements("temperature").Where(e => e.Attribute("type").Value.Equals("hourly")).First().Elements("value").ToList();
            var windDirectionNodes = root.Element("data").Element("parameters").Elements("direction").Where(e => e.Attribute("type").Value.Equals("wind")).First().Elements("value").ToList();

            var forecasts = new List<HourlyForecast>();
            for (int i = 0; i < startTimeNodes.Count; i++)
            {
                var hf = new HourlyForecast();
                hf.StartTime = DateTime.Parse(startTimeNodes[i].Value);
                hf.EndTime = DateTime.Parse(endTimeNodes[i].Value);
                hf.CloudCover = Convert.ToInt32(cloudPathNodes[i].Value);
                hf.DuePointTemperature = Convert.ToInt32(duePointTempNodes[i].Value);
                hf.GustingWindSpeed = Convert.ToInt32(gustingWindSpeedNodes[i].Value == string.Empty ? "0" : gustingWindSpeedNodes[i].Value);
                hf.Humidity = Convert.ToInt32(humidityNodes[i].Value);
                hf.PercipitationPercentage = Convert.ToInt32(percipitationNodes[i].Value);
                hf.SustainedWindSpeed = Convert.ToInt32(sustainedWindNodes[i].Value);
                hf.Temperature = Convert.ToInt32(temperatureNodes[i].Value);
                hf.WindDirection = Convert.ToInt32(windDirectionNodes[i].Value);
                forecasts.Add(hf);
            }
            
            forecast.HourlyForcasts = forecasts;
            return forecast;
        }
    }
}
